//
//  MessagesV2.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/3/11.
//

import Foundation
class MessageContentV2 : Codable{
    var image_url: UrlBean? = nil
    var type: String? = nil
    var text: String? = nil
    
    init(_ type: Int, _ text: String?, _ url: String?) {
        self.text = text
        switch(type) {
        case 0:
            self.type = "text"
            break
        case 1:
            self.type = "image_url"
            break
        case 2:
            self.type = "input_audio"
            break
        default:
            self.type = "text"
        }
        // 验证是否为有效URL
        let url = url ?? ""
        if isValidUrl(url) {
            image_url = UrlBean(url: url)
        } else {
            // 尝试从本地文件路径加载图片并转换为Base64
            guard let uiImage = ImageFileManager.loadImage(url),let base64String = uiImage.toBase64() else {
                image_url = UrlBean(url: url)
                return
            }
            
            // 构建完整的Base64数据格式
            let baseData = "data:image/jpg;base64,\(base64String)"
            // 移除可能存在的换行符
            let cleanedData = baseData.replacingOccurrences(of: "\n", with: "")
            image_url = UrlBean(url: cleanedData)
        }
    }
}
class UrlBean : Codable {
    var url :String? = nil
    init(url: String) {
        self.url = url
    }
}
